<?php

namespace app\admin\model;

use think\Model;

class Banner extends Model
{
    public function getCreatedTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public function bannerList($where, $field = [])
    {
        $list = $this->field($field)
            ->where($where)
            ->order('created_time','desc')
            ->select()->toArray();

        return $list;
    }

    public function getOne($where, $field = '*', $order = '')
    {
        return $this->field($field)->where($where)->order($order)->find();
    }

    public function delWen($id)
    {
        return $this->where(array('id' => $id))->delete();
    }

    public function getBanners($num = 20)
    {
        $keywords = input('param.keywords', '');
        $where = "";
        $param = [];
        if ($keywords) {
            $param['keywords'] = $keywords;
        }

        $articles = $this->field('*')
            ->where($where)
            ->order('id', 'DESC')
            ->paginate($num, false, ['query' => $param]);

        $page = $articles->render();// 获取分页显示

        if ($articles) {
            return ['code' => 1, 'data' => $articles, 'msg' => '数据查询成功', 'page' => $page];
        } else {
            return ['code' => 2, 'data' => '', 'msg' => '暂无数据', 'page' => ''];
        }
    }

    /**
     * @Title: editBanner
     * @Description: 编辑banner信息
     * @param $param
     * @return mixed
     * @Author: 李军伟
     */
    public function editBanner($param)
    {
        $this->allowField(true)->isUpdate(true)->save($param, ['id' => $param['id']]);

        return $this->id;
    }
}
